package com.taoyun.web.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taoyun.mapper.FileMapper;
import com.taoyun.pojo.entity.Group;
import com.taoyun.pojo.entity.User;
import com.taoyun.pojo.vo.GroupMember;
import com.taoyun.pojo.vo.GroupVO;
import com.taoyun.service.IGroupService;
import com.taoyun.service.IObsService;
import com.taoyun.service.IUserService;
import com.taoyun.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import java.awt.*;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lkxicy
 * @since 2020-09-23
 */
@RestController
public class GroupController {

    @Autowired
    private IGroupService groupService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IObsService obsService;

    @Autowired
    private FileMapper fileMapper;



    /**
     * 管理员查询自己的群组
     * @param AdminId
     * @return
     */
    @GetMapping("Group/AdminShowGroup")
    public Response AdminShowGroup(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "size",defaultValue = "5")Integer size,@RequestParam("AdminId") Integer AdminId){
        if(AdminId != null){
            PageHelper.startPage(page,size,true);
            List<Group> GroupList = groupService.ShowGroup(AdminId);  //返回该管理员所在的群组列表
            PageInfo<Group> pageInfo = new PageInfo<>(GroupList);
            if(GroupList!=null && GroupList.size()!=0) //判断返回的所属群组列表是否为null
                return Response.success("查询所属群组成功",pageInfo);
            return Response.error("您还没有群组,请先创建或加入群组！");
        }else{
            return Response.error("AdminId不能为空！");
        }
    }

    /**
     * 返回群成员所在的群组列表
     * @param UId
     * @return
     */
    @GetMapping("Group/MemberShowGroup")
    public Response MemberShowGroup(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "size",defaultValue = "8")Integer size,@RequestParam("UId") Integer UId){
        if(UId != null){
            PageHelper.startPage(page,size,true);
            List<Group> GroupList = groupService.ShowGroup(UId);  //返回该员工所在的群组列表
            PageInfo<Group> pageInfo = new PageInfo<>(GroupList);
            if(GroupList!=null && GroupList.size()!=0) //判断返回的所属群组列表是否为null
                return Response.success("查询所属群组成功",pageInfo);
            return Response.error("您还没有加入群组,请先加入群组！");
        }else{
            return Response.error("UId不能为空！");
        }
    }

    /**
     * 单个或批量删除群组成员,UId是群主的Id（判断是否为群主，否则无法操作）
     * @param UId
     * @param GroupId
     * @param Ids
     * @return
     */
    @PostMapping("Group/DeleteUserOfGroup")
    public Response DeleteUserOfGroup(@RequestParam("UId")Integer UId,@RequestParam("GroupId")Integer GroupId, @RequestBody List<Integer> Ids){
        if(UId!=null && GroupId!=null && Ids.size()!=0){
            if(groupService.SelectGroupByIdAndUId(GroupId,UId).getUPermission().equals("群主")){
                if(groupService.DeleteUserOfGroup(GroupId,Ids)==1){
                    return Response.success("删除群组成员成功！");
                }else{
                    return Response.error("删除群组成员失败！");
                }
            }else{
                return Response.error("不好意思，您不是该群的群主，无法执行该操作！");
            }
        }else if(UId!=null && GroupId!=null && Ids.size()==0){
            return Response.error("Ids参数丢失！");
        }else if(UId!=null && GroupId==null && Ids.size()!=0){
            return Response.error("GroupId参数丢失！");
        }else if(UId==null && GroupId!=null && Ids.size()!=0){
            return Response.error("UId参数丢失！");
        }else{
            return Response.error("多个参数丢失！");
        }
    }

    /**
     * 通过群组GroupId查询群的信息，包括成员信息
     * @param GroupId
     * @return List<Group> groupList
     */
    @GetMapping("Group/ShowInformationOfGroup")
    public Response ShowInformationOfGroup(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                           @RequestParam(value = "size",defaultValue = "8")Integer size,@RequestParam("GroupId")Integer GroupId){
        if(GroupId!=null){
            PageHelper.startPage(page,size,true);
            List<Group> groupMembers = groupService.ShowInformationOfGroup(GroupId);
            PageInfo<Group> pageInfo = new PageInfo<>(groupMembers);
            if(groupMembers.size()!=0 && groupMembers!=null){
                return Response.success("查询成功，群GroupId为:"+GroupId+"的信息如下",pageInfo);
            }else{
                return Response.error("没有查询到GroupId为"+GroupId+"的信息，请确认输入无误！");
            }
        }else{
            return Response.error("GroupId参数丢失！");
        }
    }

    /**
     * 超管、管理员创建群组，传入管理员的AdminId(对应数据库的User_Id)和群组的名字GroupName,群组的GroupId
     * @param AdminId 管理员编号
     * @param GroupName 群组名称
     * @param GroupId   群组编号（不是自增的那个）
     * @return
     */
    @GetMapping("Group/CreateGroup")
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Response CreateGroup(@RequestParam("AdminId") Integer AdminId, @RequestParam("GroupName") String GroupName ,
                                @RequestParam("GroupId")Integer GroupId) throws IOException {
        GroupName = URLDecoder.decode(GroupName, "utf-8");
        if(AdminId != null && !GroupName.equals("")){
            User Admin = userService.getUserById(AdminId);
            if(Admin.getPermission().equals("管理员") || Admin.getPermission().equals("超管")){
                Group AdminCreateGroup = new Group(GroupName, AdminId, GroupId);  //首先管理员创建一个群组，该初始方法默认全部权限为1
                Integer result = groupService.CreateGroup(AdminCreateGroup, AdminId);
                if(result == 1){
                    return Response.success("创建群组成功，群组ID："+GroupId+",群组名字："+GroupName);
                }else{
                    return Response.error("创建群组失败");
                }
            }else{
                return Response.error("不好意思，您不是管理员或超管，无法创建群组！");
            }
        }else if(AdminId != null && GroupName.equals("")){
            return Response.error("GroupName群组名字不能为空，请填写完整。");
        }else{
            return Response.error("操作者的Id不能为空，请填写完整！");
        }
    }

    /**
     * 返回所有用户列表，供拉人选择
     * @param AdminId
     * @return UserList
     */
    @GetMapping("Group/ShowAllUser")
    public Response ShowAllUser(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "size",defaultValue = "8")Integer size,@RequestParam("AdminId")Integer AdminId){
        if(AdminId!=null){
            User Admin = userService.getUserById(AdminId);
            if(Admin.getPermission().equals("管理员")||Admin.getPermission().equals("超管")){
                PageHelper.startPage(page,size,true);
                List<User> UserList = userService.ShowAll(AdminId); //除去自己以外的所有员工信息
                PageInfo<User> pageInfo = new PageInfo<>(UserList);
                if(UserList!=null&&UserList.size()!=0){
                    return Response.success("以下是云盘的所有用户，请从中选择您的群成员吧！",pageInfo);
                }else{
                    return Response.error("云盘目前没有用户，请先添加用户再来看看吧！");
                }
            }else{
                return Response.error("不好意思，您不是管理员或超管，无法执行该操作！");
            }
        }else{
            return Response.error("传入的AdminId参数丢失，无法判断您的身份。");
        }
    }

    /**
     * 群主拉员工进群，需要传进群主AdminId，群组GroupId，被选取的员工的Ids列表
     * http://localhost:8080/Group/SelectUserIntoGroup
     * @param AdminId
     * @param GroupId
     * @param Ids
     * @return
     */
    @PostMapping("Group/SelectUserIntoGroup")
    public Response SelectUserIntoGroup(@RequestParam("AdminId")Integer AdminId, @RequestParam("GroupId")Integer GroupId,
                                        @RequestBody List<Integer> Ids){
        if(AdminId!=null&&GroupId!=null&&Ids.size()!=0){
            Group group = groupService.SelectGroupByIdAndUId(GroupId,AdminId);
            if(group!=null && group.getUPermission().equals("群主")){
                if(groupService.SelectUserIntoGroup(Ids,GroupId,AdminId) == 1){
                    return Response.success("拉取员工进群成功！");
                }else{
                    return Response.error("拉取员工进群失败！");
                }
            }else{
                return Response.error("不好意思，您不是群主，无法执行该操作！");
            }
        }else if(AdminId!=null&&GroupId!=null&&Ids.size()==0){
            return Response.error("Ids为空，未选择群成员，请选择您的群成员！");
        }else if(AdminId!=null&&GroupId==null&&Ids.size()!=0){
            return Response.error("GroupId为空，请填写！");
        }else if(AdminId==null&&GroupId!=null&&Ids.size()!=0){
            return Response.error("AdminId为空，无法判断您的身份，暂时无法执行！");
        }else{
            return Response.error("多个参数丢失，请检查！");
        }
    }

    /**
     * 修改群成员上传权限
     * @param GroupId
     * @param AdminId
     * @param Ids
     * @param UpPermission
     * @return
     */
    @PutMapping("Group/ModifyUpPermission")
    public Response ModifyUpPermission(@RequestParam("GroupId")Integer GroupId,@RequestParam("AdminId")Integer AdminId,
                                       @RequestBody List<Integer> Ids,@RequestParam("UpPermission")String UpPermission){
        if(GroupId!=null && AdminId!=null && Ids.size()!=0 && UpPermission!=null){
            if(groupService.SelectGroupByIdAndUId(GroupId,AdminId).getUPermission().equals("群主")){
                if(groupService.ModifyUpPermission(GroupId,Ids,UpPermission)==Ids.size()){
                    return Response.success("修改成功");
                }else{
                    return Response.error("修改失败");
                }
            }else{
                return Response.error("您不是群主，无法执行该操作！");
            }
        }else if(GroupId!=null && AdminId!=null && Ids.size()!=0 && UpPermission==null){
            return Response.error("UpPermission为空");
        }else if(GroupId!=null && AdminId!=null && Ids.size()==0 && UpPermission!=null){
            return Response.error("Ids为空");
        }else if(GroupId!=null && AdminId==null && Ids.size()!=0 && UpPermission!=null){
            return Response.error("AdminId为空");
        }else if(GroupId==null && AdminId!=null && Ids.size()!=0 && UpPermission!=null){
            return Response.error("GroupId为空");
        }else{
            return Response.error("多个参数丢失");
        }
    }

    /**
     * 修改群成员下载权限
     * @param GroupId
     * @param AdminId
     * @param Ids
     * @param DlPermission
     * @return
     */
    @PutMapping("Group/ModifyDlPermission")
    public Response ModifyDlPermission(@RequestParam("GroupId")Integer GroupId,@RequestParam("AdminId")Integer AdminId,
                                       @RequestBody List<Integer> Ids,@RequestParam("UpPermission")String DlPermission){
        if(GroupId!=null && AdminId!=null && Ids.size()!=0 && DlPermission!=null){
            if(groupService.SelectGroupByIdAndUId(GroupId,AdminId).getUPermission().equals("群主")){
                if(groupService.ModifyDlPermission(GroupId,Ids,DlPermission)==Ids.size()){
                    return Response.success("修改成功");
                }else{
                    return Response.error("修改失败");
                }
            }else{
                return Response.error("您不是群主，无法执行该操作！");
            }
        }else if(GroupId!=null && AdminId!=null && Ids.size()!=0 && DlPermission==null){
            return Response.error("DlPermission为空");
        }else if(GroupId!=null && AdminId!=null && Ids.size()==0 && DlPermission!=null){
            return Response.error("Ids为空");
        }else if(GroupId!=null && AdminId==null && Ids.size()!=0 && DlPermission!=null){
            return Response.error("AdminId为空");
        }else if(GroupId==null && AdminId!=null && Ids.size()!=0 && DlPermission!=null){
            return Response.error("GroupId为空");
        }else{
            return Response.error("多个参数丢失");
        }
    }

    /**
     * 修改群成员浏览查看权限
     * @param GroupId
     * @param AdminId
     * @param Ids
     * @param LPermission
     * @return
     */
    @PutMapping("Group/ModifyLPermission")
    public Response ModifyLPermission(@RequestParam("GroupId")Integer GroupId,@RequestParam("AdminId")Integer AdminId,
                                       @RequestBody List<Integer> Ids,@RequestParam("UpPermission")String LPermission){
        if(GroupId!=null && AdminId!=null && Ids.size()!=0 && LPermission!=null){
            if(groupService.SelectGroupByIdAndUId(GroupId,AdminId).getUPermission().equals("群主")){
                if(groupService.ModifyLPermission(GroupId,Ids,LPermission)==Ids.size()){
                    return Response.success("修改成功");
                }else{
                    return Response.error("修改失败");
                }
            }else{
                return Response.error("您不是群主，无法执行该操作！");
            }
        }else if(GroupId!=null && AdminId!=null && Ids.size()!=0 && LPermission==null){
            return Response.error("LPermission为空");
        }else if(GroupId!=null && AdminId!=null && Ids.size()==0 && LPermission!=null){
            return Response.error("Ids为空");
        }else if(GroupId!=null && AdminId==null && Ids.size()!=0 && LPermission!=null){
            return Response.error("AdminId为空");
        }else if(GroupId==null && AdminId!=null && Ids.size()!=0 && LPermission!=null){
            return Response.error("GroupId为空");
        }else{
            return Response.error("多个参数丢失");
        }
    }

    /**
     * 修改群成员删除权限
     * @param GroupId
     * @param AdminId
     * @param Ids
     * @param DPermission
     * @return
     */
    @PutMapping("Group/ModifyDPermission")
    public Response ModifyDPermission(@RequestParam("GroupId")Integer GroupId,@RequestParam("AdminId")Integer AdminId,
                                       @RequestBody List<Integer> Ids,@RequestParam("UpPermission")String DPermission){
        if(GroupId!=null && AdminId!=null && Ids.size()!=0 && DPermission!=null){
            if(groupService.SelectGroupByIdAndUId(GroupId,AdminId).getUPermission().equals("群主")){
                if(groupService.ModifyDPermission(GroupId,Ids,DPermission)==Ids.size()){
                    return Response.success("修改成功");
                }else{
                    return Response.error("修改失败");
                }
            }else{
                return Response.error("您不是群主，无法执行该操作！");
            }
        }else if(GroupId!=null && AdminId!=null && Ids.size()!=0 && DPermission==null){
            return Response.error("DPermission为空");
        }else if(GroupId!=null && AdminId!=null && Ids.size()==0 && DPermission!=null){
            return Response.error("Ids为空");
        }else if(GroupId!=null && AdminId==null && Ids.size()!=0 && DPermission!=null){
            return Response.error("AdminId为空");
        }else if(GroupId==null && AdminId!=null && Ids.size()!=0 && DPermission!=null){
            return Response.error("GroupId为空");
        }else{
            return Response.error("多个参数丢失");
        }
    }

    /**
     * 修改群成员重命名权限
     * @param GroupId
     * @param AdminId
     * @param Ids
     * @param RePermission
     * @return
     */
    @PutMapping("Group/ModifyRePermission")
    public Response ModifyRePermission(@RequestParam("GroupId")Integer GroupId,@RequestParam("AdminId")Integer AdminId,
                                       @RequestBody List<Integer> Ids,@RequestParam("UpPermission")String RePermission){
        if(GroupId!=null && AdminId!=null && Ids.size()!=0 && RePermission!=null){
            if(groupService.SelectGroupByIdAndUId(GroupId,AdminId).getUPermission().equals("群主")){
                if(groupService.ModifyRePermission(GroupId,Ids,RePermission)==Ids.size()){
                    return Response.success("修改成功");
                }else{
                    return Response.error("修改失败");
                }
            }else{
                return Response.error("您不是群主，无法执行该操作！");
            }
        }else if(GroupId!=null && AdminId!=null && Ids.size()!=0 && RePermission==null){
            return Response.error("RePermission为空");
        }else if(GroupId!=null && AdminId!=null && Ids.size()==0 && RePermission!=null){
            return Response.error("Ids为空");
        }else if(GroupId!=null && AdminId==null && Ids.size()!=0 && RePermission!=null){
            return Response.error("AdminId为空");
        }else if(GroupId==null && AdminId!=null && Ids.size()!=0 && RePermission!=null){
            return Response.error("GroupId为空");
        }else{
            return Response.error("多个参数丢失");
        }
    }

    /**
     * 修改群成员移动权限
     * @param GroupId
     * @param AdminId
     * @param Ids
     * @param MPermission
     * @return
     */
    @PutMapping("Group/ModifyMPermission")
    public Response ModifyMPermission(@RequestParam("GroupId")Integer GroupId,@RequestParam("AdminId")Integer AdminId,
                                       @RequestBody List<Integer> Ids,@RequestParam("UpPermission")String MPermission){
        if(GroupId!=null && AdminId!=null && Ids.size()!=0 && MPermission!=null){
            if(groupService.SelectGroupByIdAndUId(GroupId,AdminId).getUPermission().equals("群主")){
                if(groupService.ModifyMPermission(GroupId,Ids,MPermission)==Ids.size()){
                    return Response.success("修改成功");
                }else{
                    return Response.error("修改失败");
                }
            }else{
                return Response.error("您不是群主，无法执行该操作！");
            }
        }else if(GroupId!=null && AdminId!=null && Ids.size()!=0 && MPermission==null){
            return Response.error("MPermission为空");
        }else if(GroupId!=null && AdminId!=null && Ids.size()==0 && MPermission!=null){
            return Response.error("Ids为空");
        }else if(GroupId!=null && AdminId==null && Ids.size()!=0 && MPermission!=null){
            return Response.error("AdminId为空");
        }else if(GroupId==null && AdminId!=null && Ids.size()!=0 && MPermission!=null){
            return Response.error("GroupId为空");
        }else{
            return Response.error("多个参数丢失");
        }
    }

    /**
     * 删除群组，删除相关数据库表以及Obs文件夹
     *  http://localhost:8080/taoyun/v1/Group/DeleteGroup
     * @param AdminId 该群组的管理员U_Id
     * @param GroupId  群组的GroupId
     * @return
     */
    @DeleteMapping("taoyun/v1/Group/DeleteGroup")
    public Response DeleteGroup(@RequestParam("AdminId") Integer AdminId, @RequestParam("GroupId") Integer GroupId){
        //判断权限
        String permission = groupService.SelectGroupByIdAndUId(GroupId, AdminId).getUPermission();
        if(permission.equals("群主")){
            //执行删除群组
            Boolean result = groupService.DeleteGroup(GroupId);
            if(result){
                return Response.success("删除成功！");
            }else {
                return Response.error("删除失败！程序出错！");
            }
        }else {
            return Response.error("您不是该群组的管理员，无法执行删除。");
        }
    }

    /**
     * 返回用户所加入的群组Id
     * http://localhost:8080/taoyun/v1/Group/ShowUserBelongGroup
     * @param userId 用户编号U_Id
     * @return groupIdList 群组编号列表
     *  {
            "code": "200",
            "msg": "该用户加入的群组编号如下：",
            "data": [222,316]
        }
     */
    @GetMapping("taoyun/v1/Group/ShowUserBelongGroup")
    public Response ShowUserBelongGroup(@RequestParam("userId") Integer userId){
        List<Integer> groupIdList = groupService.ShowUserBelongGroup(userId);
        if(groupIdList.size() > 0){
            return Response.success("该用户加入的群组编号如下：", groupIdList);
        }else{
            return Response.success("该用户没有加入群组");
        }
    }
}

